স্প্রিং বুট ক্লায়েন্ট RestTemplate বা WebClient ব্যবহার করে বিভিন্ন HTTP মেথড (GET, POST, PUT, DELETE, ইত্যাদি) এর মাধ্যমে API এর সাথে যোগাযোগ করতে পারে।
এখানে প্রতিটি HTTP মেথডের ব্যবহার এবং হ্যান্ডলিং নিয়ে আলোচনা করা হয়েছে:
১. GET Request
GET রিকোয়েস্ট সাধারণত ডাটা রিট্রাইভ করতে ব্যবহৃত হয়।
RestTemplate ব্যবহার:
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String getData(String url) {
return restTemplate.getForObject(url, String.class);
}
}
WebClient ব্যবহার:
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
@Service
public class ApiService {
private final WebClient webClient;
public ApiService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("http://example.com").build();
}
public String getData(String endpoint) {
return webClient.get()
.uri(endpoint)
.retrieve()
.bodyToMono(String.class)
.block(); // Reactive এর জন্য block() করা হচ্ছে
}
}
২. POST Request
POST রিকোয়েস্ট সাধারণত ডাটা সার্ভারে সাবমিট করতে ব্যবহৃত হয়।
RestTemplate ব্যবহার:
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public ResponseEntity<String> postData(String url, Object requestBody) {
return restTemplate.postForEntity(url, requestBody, String.class);
}
}
WebClient ব্যবহার:
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
@Service
public class ApiService {
private final WebClient webClient;
public ApiService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("http://example.com").build();
}
public String postData(String endpoint, Object requestBody) {
return webClient.post()
.uri(endpoint)
.bodyValue(requestBody)
.retrieve()
.bodyToMono(String.class)
.block();
}
}
৩. PUT Request
PUT রিকোয়েস্ট সাধারণত বিদ্যমান ডাটা আপডেট করতে ব্যবহৃত হয়।
RestTemplate ব্যবহার:
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public void updateData(String url, Object requestBody) {
restTemplate.put(url, requestBody);
}
}
WebClient ব্যবহার:
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
@Service
public class ApiService {
private final WebClient webClient;
public ApiService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("http://example.com").build();
}
public String updateData(String endpoint, Object requestBody) {
return webClient.put()
.uri(endpoint)
.bodyValue(requestBody)
.retrieve()
.bodyToMono(String.class)
.block();
}
}
৪. DELETE Request
DELETE রিকোয়েস্ট সাধারণত সার্ভার থেকে ডাটা মুছে ফেলতে ব্যবহৃত হয়।
RestTemplate ব্যবহার:
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public void deleteData(String url) {
restTemplate.delete(url);
}
}
WebClient ব্যবহার:
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
@Service
public class ApiService {
private final WebClient webClient;
public ApiService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("http://example.com").build();
}
public void deleteData(String endpoint) {
webClient.delete()
.uri(endpoint)
.retrieve()
.bodyToMono(Void.class)
.block();
}
}
৫. Exchange Method (Custom HTTP Method Support)
RestTemplate ব্যবহার:
exchange মেথড ব্যবহার করে কাস্টম HTTP মেথড হ্যান্ডল করতে পারেন।
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public ResponseEntity<String> exchangeRequest(String url, HttpMethod method, Object body) {
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Bearer your-token");
HttpEntity<Object> entity = new HttpEntity<>(body, headers);
return restTemplate.exchange(url, method, entity, String.class);
}
}
WebClient ব্যবহার:
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
@Service
public class ApiService {
private final WebClient webClient;
public ApiService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("http://example.com").build();
}
public String exchangeRequest(String endpoint, HttpMethod method, Object body) {
return webClient.method(method)
.uri(endpoint)
.bodyValue(body)
.retrieve()
.bodyToMono(String.class)
.block();
}
}
HTTP Methods এবং Request Handling এর প্রধান টিপস:
- Error Handling:
RestTemplateএরResponseErrorHandlerবাWebClientএরonStatus()ব্যবহার করুন। - Headers যুক্ত করা:
HttpHeadersব্যবহার করে রিকোয়েস্টে কাস্টম হেডার যুক্ত করুন। - Authentication: অথেনটিকেশন দরকার হলে হেডারে টোকেন বা ক্রেডেনশিয়াল যোগ করুন।
- Reactive API Call: যদি নন-ব্লকিং কল প্রয়োজন হয়, তাহলে
WebClientব্যবহার করুন। - Timeout: টাইমআউট নিশ্চিত করার জন্য কাস্টম ক্লায়েন্ট কনফিগার করুন।
এভাবে, আপনি স্প্রিং বুট ক্লায়েন্টে REST API এর বিভিন্ন HTTP মেথড দক্ষতার সাথে হ্যান্ডল করতে পারবেন।
Spring Boot-এ RestTemplate এবং WebClient ব্যবহার করে GET, POST, PUT, DELETE, এবং PATCH রিকোয়েস্ট পরিচালনা করা যায়। এখানে প্রতিটি রিকোয়েস্টের উদাহরণ দেওয়া হলো:
1. GET Request
GET রিকোয়েস্ট সাধারণত ডেটা রিট্রিভ করার জন্য ব্যবহৃত হয়।
RestTemplate দিয়ে:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
@Autowired
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String fetchData() {
String url = "https://api.example.com/data";
return restTemplate.getForObject(url, String.class);
}
}
2. POST Request
POST রিকোয়েস্ট নতুন ডেটা তৈরি করার জন্য ব্যবহৃত হয়।
RestTemplate দিয়ে:
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String createData(Object requestData) {
String url = "https://api.example.com/data";
return restTemplate.postForObject(url, requestData, String.class);
}
}
3. PUT Request
PUT রিকোয়েস্ট বিদ্যমান ডেটা আপডেট করার জন্য ব্যবহৃত হয়।
RestTemplate দিয়ে:
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public void updateData(Long id, Object updatedData) {
String url = "https://api.example.com/data/" + id;
restTemplate.put(url, updatedData);
}
}
4. DELETE Request
DELETE রিকোয়েস্ট বিদ্যমান ডেটা মুছে ফেলার জন্য ব্যবহৃত হয়।
RestTemplate দিয়ে:
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public void deleteData(Long id) {
String url = "https://api.example.com/data/" + id;
restTemplate.delete(url);
}
}
5. PATCH Request
PATCH রিকোয়েস্ট আংশিক আপডেট করার জন্য ব্যবহৃত হয়।
RestTemplate দিয়ে:
RestTemplate সরাসরি PATCH রিকোয়েস্ট সমর্থন করে না, তবে exchange() ব্যবহার করে এটি করা সম্ভব।
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String patchData(Long id, Object partialUpdate) {
String url = "https://api.example.com/data/" + id;
HttpEntity<Object> requestEntity = new HttpEntity<>(partialUpdate);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.PATCH, requestEntity, String.class);
return response.getBody();
}
}
WebClient ব্যবহার করে:
WebClient দিয়ে GET, POST, PUT, DELETE, PATCH রিকোয়েস্ট খুব সহজে করা যায়।
GET Example:
public Mono<String> fetchData() {
return webClient.get()
.uri("/data")
.retrieve()
.bodyToMono(String.class);
}
POST Example:
public Mono<String> createData(Object requestData) {
return webClient.post()
.uri("/data")
.bodyValue(requestData)
.retrieve()
.bodyToMono(String.class);
}
PUT Example:
public Mono<Void> updateData(Long id, Object updatedData) {
return webClient.put()
.uri("/data/{id}", id)
.bodyValue(updatedData)
.retrieve()
.bodyToMono(Void.class);
}
DELETE Example:
public Mono<Void> deleteData(Long id) {
return webClient.delete()
.uri("/data/{id}", id)
.retrieve()
.bodyToMono(Void.class);
}
PATCH Example:
public Mono<String> patchData(Long id, Object partialUpdate) {
return webClient.patch()
.uri("/data/{id}", id)
.bodyValue(partialUpdate)
.retrieve()
.bodyToMono(String.class);
}
সংক্ষেপে:
- RestTemplate: সিনক্রোনাস অপারেশনের জন্য ব্যবহৃত হয়।
- WebClient: রিঅ্যাক্টিভ প্রোগ্রামিংয়ের জন্য উপযোগী।
আপনার অ্যাপ্লিকেশনের প্রয়োজন অনুসারে GET, POST, PUT, DELETE এবং PATCH রিকোয়েস্টগুলো সহজেই পরিচালনা করা যায়।
Spring Boot-এ ResponseEntity একটি গুরুত্বপূর্ণ ক্লাস যা HTTP রেসপন্সের স্ট্যাটাস কোড, হেডার, এবং বডি পরিচালনা করতে ব্যবহৃত হয়। HTTP Methods এর মাধ্যমে ResponseEntity হ্যান্ডল করার পদ্ধতি নিম্নে আলোচনা করা হলো:
ResponseEntity ব্যবহার কেন জরুরি?
- HTTP স্ট্যাটাস কোড প্রদান করা: ক্লায়েন্টকে সঠিক HTTP স্ট্যাটাস কোড পাঠানো।
- হেডার সেট করা: রেসপন্সের জন্য কাস্টম HTTP হেডার সংযুক্ত করা।
- বডি প্রদান করা: রেসপন্স বডি পাঠানো।
- এলাস্টিক রেসপন্স: স্ট্যাটাস, হেডার, এবং বডি একসাথে হ্যান্ডল করা।
১. ResponseEntity হ্যান্ডল করা: GET Method
উদাহরণ:
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public ResponseEntity<String> getDataFromApi() {
String url = "https://jsonplaceholder.typicode.com/posts";
// API থেকে ResponseEntity গ্রহণ করা
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
return response;
}
}
Controller:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ApiController {
@Autowired
private ApiService apiService;
@GetMapping("/getPosts")
public ResponseEntity<String> getPosts() {
return apiService.getDataFromApi();
}
}
২. ResponseEntity হ্যান্ডল করা: POST Method
উদাহরণ:
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public ResponseEntity<String> postDataToApi(String jsonPayload) {
String url = "https://jsonplaceholder.typicode.com/posts";
HttpEntity<String> request = new HttpEntity<>(jsonPayload);
// POST Method-এর মাধ্যমে ডেটা পাঠানো
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
return response;
}
}
Controller:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ApiController {
@Autowired
private ApiService apiService;
@PostMapping("/createPost")
public ResponseEntity<String> createPost(@RequestBody String jsonPayload) {
return apiService.postDataToApi(jsonPayload);
}
}
৩. ResponseEntity হ্যান্ডল করা: PUT Method
উদাহরণ:
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public ResponseEntity<String> updateDataInApi(String jsonPayload, int id) {
String url = "https://jsonplaceholder.typicode.com/posts/" + id;
HttpEntity<String> request = new HttpEntity<>(jsonPayload);
// PUT Method ব্যবহার
restTemplate.put(url, request);
return new ResponseEntity<>("Resource updated successfully", HttpStatus.OK);
}
}
Controller:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ApiController {
@Autowired
private ApiService apiService;
@PutMapping("/updatePost")
public ResponseEntity<String> updatePost(@RequestBody String jsonPayload, @RequestParam int id) {
return apiService.updateDataInApi(jsonPayload, id);
}
}
৪. ResponseEntity হ্যান্ডল করা: DELETE Method
উদাহরণ:
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ApiService {
private final RestTemplate restTemplate;
public ApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public ResponseEntity<String> deleteDataFromApi(int id) {
String url = "https://jsonplaceholder.typicode.com/posts/" + id;
// DELETE Method ব্যবহার
restTemplate.delete(url);
return new ResponseEntity<>("Resource deleted successfully", HttpStatus.OK);
}
}
Controller:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ApiController {
@Autowired
private ApiService apiService;
@DeleteMapping("/deletePost")
public ResponseEntity<String> deletePost(@RequestParam int id) {
return apiService.deleteDataFromApi(id);
}
}
ResponseEntity-এর গুরুত্বপূর্ণ বৈশিষ্ট্য
Custom HTTP Status Code:
return new ResponseEntity<>("Error occurred", HttpStatus.BAD_REQUEST);Custom HTTP Headers:
HttpHeaders headers = new HttpHeaders(); headers.set("Custom-Header", "HeaderValue"); return new ResponseEntity<>("Response Body", headers, HttpStatus.OK);ResponseEntity.ok() Shortcut:
return ResponseEntity.ok("Response Body");ResponseEntity.noContent() for DELETE:
return ResponseEntity.noContent().build();
উপসংহার
Spring Boot-এ ResponseEntity ব্যবহার করে HTTP Methods এর জন্য রেসপন্স বডি, হেডার, এবং স্ট্যাটাস কোড সহজেই হ্যান্ডল করা যায়। এটি অ্যাপ্লিকেশনকে আরো লচিৎ এবং কাস্টমাইজড রেসপন্স প্রদান করতে সহায়তা করে।
যদি আরো বিস্তারিত সাহায্য প্রয়োজন হয়, জানান! 😊
স্প্রিং বুটে RestTemplate বা WebClient ব্যবহার করে বিভিন্ন HTTP মেথড যেমন GET, POST, PUT, DELETE, ইত্যাদির মাধ্যমে RESTful API-তে অনুরোধ করা যায়। নিচে উদাহরণসহ এই মেথডগুলোর ব্যবহার দেখানো হলো:
GET Method
GET মেথড সাধারণত সার্ভার থেকে ডেটা সংগ্রহের জন্য ব্যবহৃত হয়।
উদাহরণ: RestTemplate ব্যবহার করে
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class RestTemplateClient {
private final RestTemplate restTemplate;
public RestTemplateClient(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String getExample(String url) {
return restTemplate.getForObject(url, String.class);
}
}
কল করার উদাহরণ:
String response = restTemplateClient.getExample("https://jsonplaceholder.typicode.com/posts/1");
System.out.println(response);
POST Method
POST মেথড ব্যবহার করে নতুন ডেটা তৈরি করা হয় বা সার্ভারে তথ্য পাঠানো হয়।
উদাহরণ: RestTemplate ব্যবহার করে
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class PostClient {
private final RestTemplate restTemplate;
public PostClient(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String createPost(String url, Object requestBody) {
ResponseEntity<String> response = restTemplate.postForEntity(url, requestBody, String.class);
return response.getBody();
}
}
কল করার উদাহরণ:
Map<String, String> requestBody = new HashMap<>();
requestBody.put("title", "foo");
requestBody.put("body", "bar");
requestBody.put("userId", "1");
String response = postClient.createPost("https://jsonplaceholder.typicode.com/posts", requestBody);
System.out.println(response);
PUT Method
PUT মেথড ব্যবহার করে বিদ্যমান ডেটা আপডেট করা হয়।
উদাহরণ: RestTemplate ব্যবহার করে
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class PutClient {
private final RestTemplate restTemplate;
public PutClient(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public void updatePost(String url, Object requestBody) {
restTemplate.put(url, requestBody);
}
}
কল করার উদাহরণ:
Map<String, String> requestBody = new HashMap<>();
requestBody.put("id", "1");
requestBody.put("title", "updated title");
requestBody.put("body", "updated body");
requestBody.put("userId", "1");
putClient.updatePost("https://jsonplaceholder.typicode.com/posts/1", requestBody);
DELETE Method
DELETE মেথড ব্যবহার করে ডেটা মুছে ফেলা হয়।
উদাহরণ: RestTemplate ব্যবহার করে
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class DeleteClient {
private final RestTemplate restTemplate;
public DeleteClient(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public void deletePost(String url) {
restTemplate.delete(url);
}
}
কল করার উদাহরণ:
deleteClient.deletePost("https://jsonplaceholder.typicode.com/posts/1");
WebClient ব্যবহার করে সব HTTP মেথড
WebClient রিয়াক্টিভ প্রোগ্রামিংয়ের জন্য ব্যবহৃত হয়। এটি RestTemplate-এর বিকল্প।
উদাহরণ: GET, POST, PUT, DELETE
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
@Service
public class WebClientExample {
private final WebClient webClient;
public WebClientExample(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("https://jsonplaceholder.typicode.com").build();
}
// GET Method
public String getPost(int id) {
return webClient.get()
.uri("/posts/{id}", id)
.retrieve()
.bodyToMono(String.class)
.block();
}
// POST Method
public String createPost(Object requestBody) {
return webClient.post()
.uri("/posts")
.bodyValue(requestBody)
.retrieve()
.bodyToMono(String.class)
.block();
}
// PUT Method
public void updatePost(int id, Object requestBody) {
webClient.put()
.uri("/posts/{id}", id)
.bodyValue(requestBody)
.retrieve()
.toBodilessEntity()
.block();
}
// DELETE Method
public void deletePost(int id) {
webClient.delete()
.uri("/posts/{id}", id)
.retrieve()
.toBodilessEntity()
.block();
}
}
কল করার উদাহরণ:
// GET
String getResponse = webClientExample.getPost(1);
System.out.println(getResponse);
// POST
Map<String, String> requestBody = new HashMap<>();
requestBody.put("title", "foo");
requestBody.put("body", "bar");
requestBody.put("userId", "1");
String postResponse = webClientExample.createPost(requestBody);
System.out.println(postResponse);
// PUT
requestBody.put("title", "updated title");
webClientExample.updatePost(1, requestBody);
// DELETE
webClientExample.deletePost(1);
সারাংশ:
RestTemplate মেথড:
- GET:
getForObject(),getForEntity() - POST:
postForObject(),postForEntity() - PUT:
put() - DELETE:
delete()
WebClient মেথড:
- GET:
get() - POST:
post() - PUT:
put() - DELETE:
delete()
উপরের উদাহরণগুলো অনুসরণ করে আপনি স্প্রিং বুট ক্লায়েন্ট দিয়ে যেকোন HTTP মেথড ব্যবহার করতে পারবেন।
Read more